﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Data.Objects.DataClasses;
using System.Windows;
using System.ComponentModel;
using Common.Model;

namespace Common.UI.EM.Agregation
{
    public class AgregationView<E> : UserControl
        where E : EntityObject, new()
    {

        public EntityCollection<E> List
        {
            get { return (EntityCollection<E>)GetValue(ListProperty); }
            set { SetValue(ListProperty, value); }
        }

        public static readonly DependencyProperty ListProperty =
            DependencyProperty.Register("List", typeof(EntityCollection<E>), typeof(AgregationView<E>), new UIPropertyMetadata(null, new PropertyChangedCallback(List_Changed)));

        private static void List_Changed(DependencyObject o, DependencyPropertyChangedEventArgs e)
        {

        } 

        public AgregationDetailDataView CustomControl
        {
            get { return (AgregationDetailDataView)GetValue(CustomControlProperty); }
            set { SetValue(CustomControlProperty, value); }
        }

        public static readonly DependencyProperty CustomControlProperty =
            DependencyProperty.Register("CustomControl", typeof(AgregationDetailDataView), typeof(AgregationView<E>), new UIPropertyMetadata(null, new PropertyChangedCallback(CustomControl_Changed)));

        private static void CustomControl_Changed(DependencyObject o, DependencyPropertyChangedEventArgs e)
        {
            AgregationView<E> agregationView = (AgregationView<E>)o;
            agregationView.View.CustomControl = (AgregationDetailDataView)e.NewValue;
        }



        GenericAgregationPresenter<E> _presenter;
        public GenericAgregationPresenter<E> Presenter
        {
            get { return _presenter; }
            set
            {
                _presenter = value;
                this.View.DataContext = value;
            }
        }

        public IAgregationListView View
        {
            get { return (IAgregationListView)this.Content; }
            set
            {
                this.Content = value;
            }
        }

        public AgregationView()
        {
            this.InitializeView();
            this.InitializePresenter();
            this.AddColumnDefinitions();
        }

        protected virtual void InitializePresenter()
        {
            this.Presenter = new GenericAgregationPresenter<E>(this);
        }

        protected virtual void InitializeView()
        {
            this.View = new AgregationListView();

            this.InitializeDetailDataView();
        }

        protected virtual void InitializeDetailDataView()
        {

        }



        protected virtual void AddColumnDefinitions()
        {
        }

        public virtual Boolean ValidateNewEntity(E entityObject)
        {
            var str = EntityValidation.Validate(entityObject);
            if (!String.IsNullOrEmpty(str))
            {
                MessageBox.Show(str, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                return false;
            }
            return true;
        }

        public virtual bool ValidateDeleteEntity(E entityObject)
        {
            return true;
        }

        public virtual EntityObject GetSelectedEntity()
        {
            return this.View.GetSelectedEntity();
        }


        public void UpdateListView()
        {
            this.InitializeDetailDataView();

            this.View.UpdateListView();
        }

    }
}